package cn.com.sinosoft.ares.insurance.controller;

import cn.com.sinosoft.ares.common.result.Result;
import cn.com.sinosoft.ares.insurance.service.AbstractRoomInsureService;
import cn.com.sinosoft.ares.insurance.service.IInsureService;
import cn.com.sinosoft.ares.insurance.service.constant.RoomDiscountType;
import cn.com.sinosoft.ares.insurance.struct.Pack;
import cn.com.sinosoft.ares.insurance.struct.Request;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * Description: 承保服务控制层
 * Author: lishangmin
 * Created: 2018-08-01 16:30
 */
@RestController
@Slf4j
public class InsuranceController {

    private final ApplicationContext context;

    @Autowired
    public InsuranceController(ApplicationContext context) {
        this.context = context;
    }

    /**
     * 房惠险承保接口
     * @param message 投保报文，JSON格式
     * @return 保单信息
     */
    @PostMapping("/insure/room/discount")
    public Result roomDiscountInsure(@RequestBody String message){
        //转换报文
        log.info("请求报文：{}",message);
        Pack pack = JSON.parseObject(message,Pack.class);
        //暂时只支持单条承保
        Request request = pack.getRequests().get(0);
        //获取险种代码
        String riskCode = request.getRiskCode();
        log.info("RiskCode : {}",riskCode);
        //对应险种的实现beanName
        String beanName = RoomDiscountType.valueOfRiskCode(riskCode).getBeanName();
        log.info("BeanName : {}",beanName);
        //从Spring容器获取实现
        IInsureService<String,Request> insureService = context.getBean(beanName,AbstractRoomInsureService.class);
        String result = insureService.insure(request);
        return Result.success();
    }

}
